﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentManagement.Models;
using System.Windows.Data;
using System.ComponentModel;
using Microsoft.Win32;
using System.Windows;
using System.Data;
using System.IO;

namespace DocumentManagement.ViewsModels
{
   public class CreateDocumentOUTViewsModel : ViewsModelBase
    {
       string filename;

        private QSHCEntities1 _entityModels;
        public QSHCEntities1 EntityModels
        {
            get { return _entityModels; }
            set
            {
                if (_entityModels != value)
                {
                    _entityModels = value;
                    OnPropertyChanged("EntityModels");
                }
            }
        }
   

        private CreateSend _myCreateSend;
        public CreateSend MyCreateSend
        {
            get { return _myCreateSend; }
            set
            {
                if (_myCreateSend != value)
                {
                    _myCreateSend = value;
                    OnPropertyChanged("MyCreateSend");
                    if (MyCreateSend != null)
                        CreateAttachedSendICV = CollectionViewSource.GetDefaultView(MyCreateSend.CreateSendAttacheds.Where(p => p.Active == true));
                }
            }
        }
    


        private CreateSendAttached _selectedAttached;
        public CreateSendAttached SelectedAttached
        {
            get { return _selectedAttached; }
            set
            {
                if (_selectedAttached != value)
                {
                    _selectedAttached = value;
                    OnPropertyChanged("SelectedAttached");
                }
            }
        }
      

        private ICollectionView _CreateAttachedSendICV;
        public ICollectionView CreateAttachedSendICV
        {
            get { return _CreateAttachedSendICV; }
            set
            {
                if (_CreateAttachedSendICV != value)
                {
                    _CreateAttachedSendICV = value;
                    OnPropertyChanged("CreateAttachedSendICV");
                }
            }
        }

        private string _myTextAttached;
        public string MyTextAttached
        {
            get { return _myTextAttached; }
            set
            {
                if (_myTextAttached != value)
                {
                    _myTextAttached = value;
                    OnPropertyChanged("MyTextAttached");
                }
            }
        }
        private string _myTextFilePath;
        public string MyTextFilePath
        {
            get { return _myTextFilePath; }
            set
            {
                if (_myTextFilePath != value)
                {
                    _myTextFilePath = value;
                    OnPropertyChanged("MyTextFilePath");
                }
            }
        }
        private bool _mySpeed1;
        public bool MySpeed1
        {
            get { return _mySpeed1; }
            set
            {
                if (_mySpeed1 != value)
                {
                    _mySpeed1 = value;
                    OnPropertyChanged("MySpeed1");
                }
            }
        }
        private bool _mySpeed2;
        public bool MySpeed2
        {
            get { return _mySpeed2; }
            set
            {
                if (_mySpeed2 != value)
                {
                    _mySpeed2 = value;
                    OnPropertyChanged("MySpeed2");
                }
            }
        }
        private bool _mySpeed3;
        public bool MySpeed3
        {
            get { return _mySpeed3; }
            set
            {
                if (_mySpeed3 != value)
                {
                    _mySpeed3 = value;
                    OnPropertyChanged("MySpeed3");
                }
            }
        }
        private bool _mySpeed4;
        public bool MySpeed4
        {
            get { return _mySpeed4; }
            set
            {
                if (_mySpeed4 != value)
                {
                    _mySpeed4 = value;
                    OnPropertyChanged("MySpeed4");
                }
            }
        }

  
       public CreateDocumentOUTViewsModel ()
	{
            //MyCreateSend = new CreateSend { CreateSendDate = DateTime.Now };
            //if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            //{
            //    EntityModels = new QSHCEntities1();

            //}
	}

     
        public void GetBrowse()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = ".txt";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                // Open document

                MyTextFilePath = dlg.FileName;
                filename = (DateTime.Today.Year + 543).ToString("D4") + DateTime.Today.Month.ToString("D2") + DateTime.Today.Day.ToString("D2") + "_" + dlg.SafeFileName;
                

            }
        }
        public void AddToDatagrid()
        {
            var AddTo = new CreateSendAttached { NameAttached = MyTextAttached, FileAttached = filename, CreateSendFilePath = MyTextFilePath, Active = true };
            MyCreateSend.CreateSendAttacheds.Add(AddTo);
            CreateAttachedSendICV.Refresh();
        }
      

        public void SaveToData()
        {
            //if (MySpeed1 == false && MySpeed2 == false && MySpeed3 == false && MySpeed4 == false)
            //{
            //    MessageBox.Show("กรุณาเลือกชั้นความเร็วของหนังสือ", "กรอกข้อมูลไม่ครบ");
            //    return;
            //}
                //ชั้นความเร็ว
                if (MySpeed1 == true) MyCreateSend.Speed = 1;
                else if (MySpeed2 == true) MyCreateSend.Speed = 2;
                else if (MySpeed3 == true) MyCreateSend.Speed = 3;
                else if (MySpeed4 == true) MyCreateSend.Speed = 4;

                if (MyCreateSend.EntityState == EntityState.Added || MyCreateSend.EntityState == EntityState.Detached) //detached ดึงข้อมูลมาจากดาต้าเบสแล้วมีการแก้ไข
                {
                    MyCreateSend.LogData = CreateLogData(LogAction.Create, MyCreateSend.CreateSendID);
                  
                    MyCreateSend.Action = 1;
                    MyCreateSend.Type = 1; // 1 คือการสร้างหนังสือออกภายนอก

                    MyCreateSend.Active = true;
                    EntityModels.CreateSends.AddObject(MyCreateSend);

                   
                }
                else if (MyCreateSend.EntityState == EntityState.Detached)// มีการแก้ไขข้อมูล
                {
                   
                    MyCreateSend.LogData = CreateLogData(LogAction.Edit, MyCreateSend.CreateSendID);

                  
                }


                foreach (var item in MyCreateSend.CreateSendAttacheds)
                {
                    if (item.EntityState == EntityState.Added || item.EntityState == EntityState.Detached)
                    {
                        File.Copy(item.CreateSendFilePath, "D:\\uploadCreateSend\\" + item.FileAttached, true);
                    }
                    else if (item.EntityState == EntityState.Modified)
                    {
                        if (File.Exists("D:\\uploadCreateSend\\" + item.FileAttached))
                        {
                            File.Delete("D:\\uploadCreateSend\\" + item.FileAttached);
                        }
                    }
                }
             

            EntityModels.SaveChanges();

            



                      //Clear Data And Autonumber
            //MyCreateSend = new CreateSend { CreateSendDate = DateTime.Now };
            //GetAutoNumber();
        }

     

      


        public void DeleteFile()
        {
            SelectedAttached.Active = false;

            if (SelectedAttached.EntityState == EntityState.Added || SelectedAttached.EntityState == EntityState.Detached)
            {
                MyCreateSend.CreateSendAttacheds.Remove(SelectedAttached);
                CreateAttachedSendICV.Refresh();
            }
            else if (SelectedAttached.EntityState == EntityState.Modified)
            {
                CreateAttachedSendICV.Refresh();
            }

        }


   
       
        public LogData CreateLogData(LogAction logAction, int primaryKeyID)
        {
            LogData logData = new LogData
            {
                Employee = EntityModels.Employees.Where(p => p.EmployeeCode == "IT007").SingleOrDefault(),
                Action = (byte)logAction,
                LogDateTime = DateTime.Now,
                SchemaName = "BackOffice",
                TableName = "CreateSends",
                PrimaryKeyID = primaryKeyID,
            };
            return logData;
        }

        public enum LogAction
        {
            Login = 1,
            Logout,
            OpenPage,
            Create,
            Edit,
            Delete,
        }

    }
}
